/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.GmnDao;
import gemini.basic.dao.PaymentDao;
import gemini.basic.model.Payment;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class PaymentDaoImpl extends HibernateDaoSupport implements PaymentDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public Payment saveOrUpdate(Payment payment, boolean flush) {
		Payment result = null;
		if (flush) {
			result = (Payment) gmnDao.saveOrUpdateAndFlush(payment);
		} else {
			result = (Payment) gmnDao.saveOrUpdate(payment);
		}
		return result;
	}

	@Override
	public List<Payment> getAllUncompletePayments() {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Payment.class);

		dtCri.add(Restrictions.isNull("dateOfComplete"));

		List<Object> paymentList = gmnDao.findByCriteria(dtCri);

		List<Payment> result = null;
		if (paymentList != null && !paymentList.isEmpty()) {
			result = new ArrayList<Payment>(paymentList.size());
			Payment pay = null;
			for (Object rsObject : paymentList) {
				pay = (Payment) rsObject;
				result.add(pay);
			}
		}
		return result;
	}

}
